package nl.cmkprojects.sage.slimplayer;

import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.Map;

import nl.cmkprojects.sage.web.ArtistInfo;
import nl.cmkprojects.sage.web.ArtistInfoRetriever;
import nl.cmkprojects.sage.web.ArtistNews;
import nl.cmkprojects.sage.web.ArtistNewsRetriever;
import nl.cmkprojects.sage.web.WebHarvestArtistInfoRetriever;
import nl.cmkprojects.sage.web.WebHarvestArtistNewsRetriever;

import org.apache.commons.collections.map.LRUMap;
import org.apache.log4j.Logger;

public class ArtistInfoManager {
  private static Logger logger = Logger.getLogger(ArtistInfoManager.class);
  private static final int CACHE_SIZE = 100;
  private static final long ARTISTINFO_DURABILITY = 3600000; // 1 hour

  @SuppressWarnings("unchecked")
  private Map<String, ArtistInfo> artistInfoCache = Collections.synchronizedMap(new LRUMap(CACHE_SIZE));

  private ArtistAliasList aliasList = ArtistAliasList.getInstance();

  public ArtistInfo getArtistInfo(String artist, String title) {
    ArtistAlias artistAlias = aliasList.getAlias(artist, title);
    artist = artistAlias != null ? artistAlias.getAlias() : artist;
    return (ArtistInfo) artistInfoCache.get(artist);
  }

  public void updateArtistInfo(String artist, String title) {
    logger.debug("updateArtistInfo(artist=" + artist + ")");
    final ArtistAlias artistAlias = aliasList.getAlias(artist, title);
    artist = artistAlias != null ? artistAlias.getAlias() : artist;
    ArtistInfo artistInfo = (ArtistInfo) artistInfoCache.get(artist);
    if (artistInfo == null || artistInfo.getTimestamp() + ARTISTINFO_DURABILITY > System.currentTimeMillis()) {
      final String artistName = artist;
      Thread t = new Thread(new Runnable() {
        public void run() {
          try {
            logger.debug("calling InfoRetriever for: " + artistName);
            ArtistInfoRetriever infoRetriever = new WebHarvestArtistInfoRetriever();
            ArtistInfo newArtistInfo = infoRetriever.getInfo(artistName);

            if (artistAlias != null && artistAlias.getCoverart() != null) {
              switch (artistAlias.getCoverart().getType()) {
              case replace:
                newArtistInfo.getPhotos().clear();
                newArtistInfo.getPhotos().addAll(0, artistAlias.getCoverart().getUrls());
                break;
              case fallback:
                if (newArtistInfo.getPhotos().isEmpty()) {
                  newArtistInfo.getPhotos().addAll(0, artistAlias.getCoverart().getUrls());
                }
                break;
              case add:
                newArtistInfo.getPhotos().addAll(0, artistAlias.getCoverart().getUrls());
                break;
              }
            }
            if (newArtistInfo != null) {
              artistInfoCache.put(artistName, newArtistInfo);
              logger.debug("calling NewsRetriever for: " + artistName);
              ArtistNewsRetriever newsRetriever = new WebHarvestArtistNewsRetriever();
              ArtistNews newArtistNews = newsRetriever.getNews(artistName);
              newArtistInfo.setNews(newArtistNews);
            }
          } catch (FileNotFoundException e) {
            logger.error("failed to run artistInforetriever", e);
          }
        }
      });
      t.start();
      return;
    }
  }
}
